﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using JsalesModel;

namespace JSales.ModuleSystem
{
    public partial class FrmRights : Form
    {
        public FrmRights()
        {
            InitializeComponent();
        }

        BindingSource binding=new BindingSource();
        private EditMode mode;

        private enum EditMode
        {
            Normal = 0,
            Edit = 1,
            Add = 2
        }

        private void ChangeEditMode(EditMode editMode)
        {
            switch (editMode)
            {
                case EditMode.Normal:
                    {
                        mode = EditMode.Normal;

                        lookUpEditRoles.Enabled = false;
                        lookUpEditUsers.Enabled = false;
                    }
                    break;
                case EditMode.Edit:
                    break;
                case EditMode.Add:
                    {
                        mode = EditMode.Add;

                        lookUpEditRoles.Enabled = true;
                        lookUpEditUsers.Enabled = true;
                    }
                    break;
            }
        }


        private void InitGrid()
        {
            using (JYEntities dbContext = new JYEntities())
            {
                IQueryable<TbRight> tbRights = from i in dbContext.TbRights
                                               select i;
                if (tbRights.Any())
                    binding.DataSource = tbRights.ToList();

            }
            BindData();
        }

        private void BindData()
        {
            gridControlRights.DataSource = binding;

            lookUpEditRoles.Properties.DataSource = GlobalVar.dtRoleName;
            lookUpEditUsers.Properties.DataSource = GlobalVar.GUsers;
            repositoryItemLookUpEditRole.DataSource = GlobalVar.dtRoleName;
            repositoryItemLookUpEditUser.DataSource = GlobalVar.GUsers;
        }

        private void InitEditZone()
        {
            lookUpEditRoles.DataBindings.Clear();
            lookUpEditUsers.DataBindings.Clear();
            textBoxID.DataBindings.Clear();

            if (binding.Count > 0)
            {
                lookUpEditRoles.DataBindings.Add("EditValue", binding, "RoleID");
                lookUpEditUsers.DataBindings.Add("EditValue", binding, "UserID");
                textBoxID.DataBindings.Add("Text", binding, "RightID");
            }
        }

        private void FrmRights_Load(object sender, EventArgs e)
        {
            ChangeEditMode(EditMode.Normal);
            InitGrid(); 
            InitEditZone();
        }

        private void barButtonItemAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            lookUpEditRoles.DataBindings.Clear();
            lookUpEditUsers.DataBindings.Clear();
            textBoxID.DataBindings.Clear();

            lookUpEditRoles.EditValue = null;
            lookUpEditUsers.EditValue = null;
            textBoxID.Text = null;

            ChangeEditMode(EditMode.Add);
        }

        private void simpleButtonGrant_Click(object sender, EventArgs e)
        {
            using (JYEntities dbContext = new JYEntities())
            {
                TbRight right=new TbRight();
                right.RoleID = int.Parse(lookUpEditRoles.EditValue.ToString());
                right.UserID = Int32.Parse(lookUpEditUsers.EditValue.ToString());

                dbContext.Add(right);
                dbContext.SaveChanges();
                
                ChangeEditMode(EditMode.Normal);
                FrmRights_Load(sender,e);
            }
        }

        private void barButtonItemDel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (!string.IsNullOrEmpty(textBoxID.Text))
            {
                using (JYEntities dbContext = new JYEntities())
                {
                    var r = from entity in dbContext.TbRights
                            where entity.RightID == int.Parse(textBoxID.Text)
                            select entity;
                    if (r.Any())
                    {
                        dbContext.Delete(r.FirstOrDefault());
                        dbContext.SaveChanges();
                        InitGrid();
                    }
                }
            }
        }




    }
}
